package com.example.controller;

import com.example.pojo.HumidityHistoryData;
import com.example.pojo.Result;
import com.example.pojo.TemperatureHistoryData;
import com.example.service.HistoryDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/history")
public class HistoryDataController {

    @Autowired
    private HistoryDataService historyDataService;

    /**
     * 获取温度历史数据
     */
    @GetMapping("/temperature")
    public Result getTemperatureData(@RequestHeader(value = "token", required = false) String token, String time) throws Exception {
        log.info("获取温度历史数据");
        if (token == null || token.isEmpty()) {
            return Result.error("404, 请求头不合法");
        }
        TemperatureHistoryData temperatureHistoryData = historyDataService.getTemperatureData(token, time);

        return Result.success(temperatureHistoryData);
    }

    /**
     * 获取湿度历史数据
     */
    @GetMapping("/humidity")
    public Result getHumidityData(@RequestHeader(value = "token", required = false) String token, String time) throws Exception {
        log.info("获取湿度历史数据");
        if (token == null || token.isEmpty()) {
            return Result.error("404, 请求头不合法");
        }
        HumidityHistoryData humidityHistoryData =historyDataService.getHumidityData(token, time);

        return Result.success(humidityHistoryData);
    }

}
